<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
use app\api\model\AdminLog as AdminLogModel;

if (!function_exists('list_to_tree')) {
    /**
     * 集合转成树状结构
     *
     * @param array $list 要转换的结果集
     * @param string $parent_id parent标记字段
     * @param string $child 子集合键名
     * @param int $root 顶级parent_id的值
     */
    function list_to_tree($list, $pk = 'id', $pid = 'parent_id', $child = 'children', $root = 0)
    {
        //创建Tree
        $tree = array();

        if (is_array($list)) {
            //创建基于主键的数组引用
            $refer = array();

            foreach ($list as $key => $data) {
                $refer[$data[$pk]] = &$list[$key];
            }

            foreach ($list as $key => $data) {
                //判断是否存在parent
                $parantId = $data[$pid];

                if ($root == $parantId) {
                    $tree[] = &$list[$key];
                } else {
                    if (isset($refer[$parantId])) {
                        $parent = &$refer[$parantId];
                        $parent[$child][] = &$list[$key];
                    }
                }
            }
        }

        return $tree;
    }
}

if (!function_exists('write_admin_log')) {
    /**
     * 记录日志
     *
     * @param string $content 日志内容
     */
    function write_admin_log($content,$detail=null,$rul1=null,$rul2=null,$rul3=null)
    {
        $admin = session('admin');
        $adminLog = new AdminLogModel();
        $data = [];
        $data['admin_id'] = $admin['id'];
        $data['admin_name'] = $admin['realname'];
        $data['content'] =  $content;
        $data['detail_content'] =  $detail;
        $data['rule1'] =  $rul1;
        $data['rule2'] =  $rul2;
        $data['rule3'] =  $rul3;
        $data['ip'] = request()->ip();
        $data['create_time'] = time();
        $adminLog->save($data);
    }
}

if (!function_exists('upload')) {
    /**
     * 上传文件
     *
     * @param string $name
     * @param integer $type 1图片;2excel;3word;
     * @return void
     */
    function upload($type = 1)
    {
        $savename=true;
        if ($type == 1) {
            $path = 'public/uploads/image';
            $validate = [
                // 'size' => 10240,
                'ext' => 'jpg,png,jpeg,raw,heic,WebP,TIF,GIF,JPEG,JPE,PNG,pdf,ai'
            ];
        }
        if ($type == 2) {
            $path = 'public/uploads/excel';
            $validate = [
                // 'size' => 10240,
                'ext' => 'xls,xlsx'
            ];
        }
        if ($type == 3) {
            $path = 'public/uploads/word';
            $validate = [
                // 'size' => 10240,
                'ext' => 'doc,docx'
            ];
        }
        if ($type == 4) {
            $savename=false;
            $path = 'public/contractTemp';
            $validate = [
                // 'size' => 10240,
                'ext' => 'png,jpg,pdf'
            ];
        }
        if ($type == 5) {
            $savename=false;
            $path = 'public/active';
            $validate = [
                // 'size' => 10240,
                'ext' => 'jpg,png,rar,zip,doc,docx,ppt,xls,pdf,txt,ai'
            ];
        }
        if ($type == 6) {
            $path = 'public/uploads/video';
            $validate = [
                // 'size' => 10240,
                'ext' => 'mp4'
            ];
        }
        $file = request()->file('file');
        if (!$file) {
            return ['code' => 0, 'msg' => '文件有误'];
        }
        $info = $file->validate($validate)->move(ROOT_PATH . $path,$savename);
        if ($info) {
            return ['code' => 1, 'msg' => '上传成功', 'data' => $info->getSaveName()];
        } else {
            // 上传失败获取错误信息
            return ['code' => 0, 'msg' => $file->getError()];
        }
    }
}
/**
 * 获取参数
 */
function getvar($key)
{
    $config = model('Config');
    return $config->where('name', $key)->value('value');
}

/**
 * 设置参数
 */
function setvar($key, $val = '')
{
    $config = model('Config');
    if (empty($key)) {
        return false;
    }
    return $config->where('name', $key)->save(['value' => $val]);
}

/**
 * 添加跟踪日志
 */
function setFollowLog($data)
{
    $data['create_time'] = time();
    return model('FollowLog')->insertData($data);
}

if (!function_exists('deep_in_array')) {
    /**
     * 判断二维数组是否存在某个值
     */
    function deep_in_array($value, $array)
    {
        foreach ($array as $item) {
            if (!is_array($item)) {
                if ($item == $value) {
                    return true;
                } else {
                    continue;
                }
            }

            if (in_array($value, $item)) {
                return true;
            } else if (deep_in_array($value, $item)) {
                return true;
            }
        }
        return false;
    }
}

//获取所属组织的所有下级的id
function xj_zz_ids($ord_id){
    //查询下级数据
    $arr = model('organization')->where(['parent_id'=>$ord_id])->select();
    $ids = array();
    foreach($arr as $v){
        if($v['parent_id'] == $ord_id){
            $ids[] = $v['id'];
            $ids = array_merge($ids,xj_zz_ids($v['id']));
        }
    }
    return $ids;
}

// st
//get请求
function getHttp($url){
    $ch = curl_init();
    //设置传输地址
    curl_setopt($ch, CURLOPT_URL, $url);
    //设置以文件流形式输出
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //接收返回数据
    $data = curl_exec($ch);
    curl_close($ch);
    $jsonInfo = json_decode($data, true);
    return $jsonInfo;
}

function http_request($url,$data=array()){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    // POST数据
    curl_setopt($ch, CURLOPT_POST, 1);
    // 把post的变量加上
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

function cdnurl($url) {
    return $url ? base_url . 'public'. $url : '';
}

function real_path($url) {
    return dirname(APP_PATH).'/public' . $url;
}


